package com.say.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.say.dao.BaseQueryDao;
import com.say.framework.core.persistence.PagingHibernateJdbcDao;


@Repository
public class BaseQueryImpl implements  BaseQueryDao {
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findChatByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.* from chat t");
        selSql.append(" WHERE t.`status`=1 ");
        if(!"".equals(params.get("userId"))){
        	selSql.append(" AND ( ");
            selSql.append(" (t.user_id=? AND t.to_user_id=?) OR (t.user_id=? AND t.to_user_id=? ) ");
            queryParams.add(params.get("userId"));
            queryParams.add(params.get("toUserId"));
            
            queryParams.add(params.get("toUserId"));
            queryParams.add(params.get("userId"));
          
            selSql.append(" ) ");
        }
        if(!"".equals(params.get("maxChatId"))){
            selSql.append(" AND t.id>?");
            queryParams.add(params.get("maxChatId"));
        }
        
        selSql.append(" ORDER BY t.create_time ASC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findMessageByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,u.wx_name as u_wx_name,u.mood as u_mood,u.weixin_photo as u_weixin_photo from message t ");
        selSql.append(" LEFT JOIN `user` u ON t.user_id=u.id ");
        selSql.append(" WHERE t.`status`=1 ");
        if(!"".equals(params.get("userId"))){
            selSql.append("  AND (t.to_user_id=?)  ");
            queryParams.add(params.get("userId"));
        }
        selSql.append(" ORDER BY t.update_time desc ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findShareByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,u.wx_name,u.weixin_photo from `share` t  ");
        selSql.append(" LEFT JOIN `user` u ON t.user_id=u.id ");
        selSql.append(" LEFT JOIN friends f ON t.user_id=f.user_id ");
        selSql.append(" where t.`status`=1  ");
        if(!"".equals(params.get("userId"))){
            selSql.append("  AND (f.user_id=?  OR f.to_user_id=? )  ");
            queryParams.add(params.get("userId"));
            queryParams.add(params.get("userId"));
        }
        selSql.append(" ORDER BY t.create_time desc");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	
	

}
